package com.asset.dispoal.company.service.commom;

import com.asset.dispoal.company.domain.db.mapper.SysCompanyMapper;
import com.asset.dispoal.company.dto.CompanyCourtAreaInsert;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tech.mhuang.pacebox.core.ddd.BaseCommand;
import tech.mhuang.pacebox.core.sugar.Attempt;
import tech.mhuang.pacebox.core.util.StringUtil;
import tech.mhuang.pacebox.springboot.protocol.Result;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Slf4j
@Component
public class InertCourtAreaCommand implements BaseCommand<CompanyCourtAreaInsert, Result> {

    @Autowired
    SysCompanyMapper companyMapper;

    public Result execute(CompanyCourtAreaInsert companyCourtAreaInsert) {
        companyMapper.deleteCourtArea(companyCourtAreaInsert.getCourtId());
        Attempt.supply(() -> {
            List<String> areaCodeList = Stream.of(StringUtil.split(companyCourtAreaInsert.getAreaCodeList(), ",")).collect(Collectors.toList());
            for (String areaCode : areaCodeList) {
                companyMapper.insertCourtArea(companyCourtAreaInsert.getCourtId(), areaCode);
            }
            return null;
        }, (ex) -> {
            log.debug("清除所有得选择");
            return null;
        }).get();

        return Result.ok();
    }
}
